package com.ruoyi.web.controller.zsix;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.process.DeploymentInfo;
import com.ruoyi.common.core.domain.process.FlowSort;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsix.domain.model.ProcessDefinitionParams;
import com.ruoyi.zsix.domain.model.TaskDefinition;
import com.ruoyi.zsix.service.IProcessDefinitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/deployment")
public class ProcessDefinitionController extends BaseController {
    @Autowired
    private IProcessDefinitionService iProcessDefinitionService;

    /**
     * 获取流程分类
     * @return
     */
    @Log(title = "获取流程分类列表" , businessType = BusinessType.EXPORT)
    @GetMapping("/getFlowSort")
    public TableDataInfo getFlowSort(){
        startPage();
        List<FlowSort> list = iProcessDefinitionService.getFlowSort();
        return getDataTable(list);
    }

    /**
     * 获取流程分类树型结构数据
     * @return
     */
    @Log(title = "获取流程分类树形结构数据" , businessType = BusinessType.EXPORT)
    @GetMapping("/getFlowSortTree")
    public AjaxResult getFlowSortTree(){
        try {
            return success(iProcessDefinitionService.getFlowSortTree());
        }
        catch (Exception ex){
            return error(ex.getMessage());
        }
    }
    /**
    获取流程列表
     */
    @Log(title = "获取流程列表" , businessType = BusinessType.EXPORT)
    @PostMapping("/getDeployment")
    public AjaxResult getDeployment(@RequestBody DeploymentInfo deploymentInfo){
        return success(iProcessDefinitionService.getDeployment(deploymentInfo));
    }

    /**
     * 流程发布
     * @param param
     * @return
     */
    @Log(title = "流程发布/更新" , businessType = BusinessType.EXPORT)
    @PostMapping(value = "/deployWithBPMNJS")
    public AjaxResult deployWithBPMNJS(@RequestBody JSONObject param){
        String stringBPMNXml=param.get("stringBPMNXml").toString();
        return success(iProcessDefinitionService.deployWithBPMNJS(stringBPMNXml));
    }
    /**
     * 获取流程定义的xml模版
     * @param response
     * @param deploymentId
     */
    @Log(title = "获取流程定义的xml模版" , businessType = BusinessType.EXPORT)
    @GetMapping(value="/getDeploymentXmlById")
    public void getDeploymentXmlById(HttpServletResponse response,
                                     @RequestParam("deploymentId") String deploymentId){
        iProcessDefinitionService.getDeploymentXmlById(response, deploymentId);
    }

    /**
     * 获取流程属性
     * @param process_key
     * @return
     */
    @Log(title = "获取流程属性" , businessType = BusinessType.EXPORT)
    @GetMapping(value="/processProperties")
    public AjaxResult processProperties(@RequestParam("process_key") String process_key){
        return success(iProcessDefinitionService.processProperties(process_key));
    }

    /**
     * 更新流程属性
     * @param processDefinitionParams
     * @return
     */
    @Log(title = "更新流程属性" , businessType = BusinessType.EXPORT)
    @PostMapping(value = "/saveProcessProperties")
    public AjaxResult saveProcessProperties(@RequestBody ProcessDefinitionParams processDefinitionParams){
        return success(iProcessDefinitionService.saveProcessProperties(processDefinitionParams));
    }
    @Log(title = "获取节点属性" , businessType = BusinessType.EXPORT)
    @GetMapping(value="/userTaskProperties")
    public AjaxResult userTaskProperties(@RequestParam("task_def_key") String task_def_key){
        return success(iProcessDefinitionService.userTaskProperties(task_def_key));
    }
    @Log(title = "更新节点属性" , businessType = BusinessType.EXPORT)
    @PostMapping(value = "/saveUserTaskProperties")
    public AjaxResult saveUserTaskProperties(@RequestBody TaskDefinition taskDefinitionParams){
        return success(iProcessDefinitionService.saveUserTaskProperties(taskDefinitionParams));
    }

    /**
     * 获取发起列表
     * @return
     */
    @Log(title = "获取流程发起列表" , businessType = BusinessType.EXPORT)
    @GetMapping(value="/getSatrtPageData")
    public AjaxResult getSatrtPageData(){
        return success(iProcessDefinitionService.getSatrtPageData());
    }
    /**
     * 增加流程分类
     * @param flowSort
     * @return
     */
    @Log(title = "增加流程分类" , businessType = BusinessType.EXPORT)
    @PostMapping(value = "/addFlowSort")
    public AjaxResult addFlowSort(@RequestBody FlowSort flowSort){
        return success(iProcessDefinitionService.addFlowSort(flowSort));
    }
    /**
     * 修改流程分类
     * @param flowSort
     * @return
     */
    @Log(title = "修改流程分类" , businessType = BusinessType.EXPORT)
    @PostMapping(value = "/saveFlowSort")
    public AjaxResult saveFlowSort(@RequestBody FlowSort flowSort){
        return success(iProcessDefinitionService.saveFlowSort(flowSort));
    }
    /**
     * 删除流程分类
     * @param flowSort
     * @return
     */
    @Log(title = "删除流程分类" , businessType = BusinessType.EXPORT)
    @PostMapping(value = "/deleteFlowSort")
    public AjaxResult deleteFlowSort(@RequestBody FlowSort flowSort){
        return success(iProcessDefinitionService.deleteFlowSort(flowSort));
    }

    /**
     * 删除流程定义
     * @param response
     * @param deploymentId
     * @return
     */
    @Log(title = "删除流程定义" , businessType = BusinessType.EXPORT)
    @GetMapping("/delDefinition")
    public AjaxResult delDefinition(HttpServletResponse response,
                                   @RequestParam("depID") String deploymentId){
        try{
            return success(iProcessDefinitionService.deleteDefinition(deploymentId));
        }
        catch (Exception ex){
            return error(ex.getMessage());
        }
    }
}
